package sample.note;

import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.Map;

import lombok.SneakyThrows;
import sample.note.storage.Repository;

import static sample.note.storage.Repository.COLUMN_TIMESTAMP;

public class SearchListFragment extends Fragment
        implements SwipeRefreshLayout.OnRefreshListener
        ,AdapterView.OnItemClickListener
{

    private SwipeRefreshLayout layout;
    private ListView lv;
    private Repository repo;
    private NotesAdapter adapter;

    @SneakyThrows
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // repository
        repo = new Repository(getContext());
        // fetch data
        ArrayList<Map<String, String>> list = repo.select();
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_search_list, container, false);
        // Binding resources Array to ListAdapter
        lv = view.findViewById(R.id.listView);
        adapter = new NotesAdapter(list, getContext());
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);

        // pull to refresh
        layout = view.findViewById(R.id.pullToRefresh);
        layout.setOnRefreshListener(this);

        FloatingActionButton fab = view.findViewById(R.id.fab);
        // context menu
        registerForContextMenu(fab);
        fab.setOnClickListener(v -> {
            getActivity().openContextMenu(fab);
        });

        return view;

    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getActivity().getMenuInflater();
        inflater.inflate(R.menu.search_list_menu, menu);
        menu.setHeaderTitle("Please Select:");
    }
    @SneakyThrows
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        int selected = item.getItemId();

        if (selected == R.id.memo) {
            // insert record
            Intent i= new Intent(getContext(), InsertActivity.class);
            startActivity(i);
            return true;
        }
        if (selected == R.id.todo) {
            //Intent i= new Intent(getContext(), EditTaskActivity.class);
            //startActivity(i);
            AppWrapper.insertEvent();
            return true;
        }
        return false;
    }

    @SneakyThrows
    @Override
    public void onRefresh() {
        ArrayList<Map<String, String>> list = repo.select();
        layout.setRefreshing(false);
        adapter.notifyChanged(list);
    }

    @SneakyThrows
    @Override
    public void onResume() {
        super.onResume();

        ArrayList<Map<String, String>> list = repo.select();
        adapter.notifyChanged(list);
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent insert = new Intent(getContext(), UpdateActivity.class);
            Map<String, String> data = adapter.getItem(position);
            assert data != null;
            String timestamp = data.get(COLUMN_TIMESTAMP);
            insert.putExtra("timestamp", timestamp);
            startActivity(insert);
    }
}
